package com.ybwh.springboot2.seq.service.impl;

import com.google.common.base.Preconditions;
import com.ybwh.springboot2.common.redis.RedisClient;
import com.ybwh.springboot2.seq.service.SeqNoService;
import com.ybwh.springboot2.util.PlaceholderUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 * 业务序列号信息服务接口实现类
 * </p>
 *
 * @author youbeiwuhuan
 * @version V1.0
 * @since 2023-06-07 11:50:26
 */
@Slf4j
@Service
public class SeqNoServiceImpl implements SeqNoService {


    @Autowired
    private RedisClient redisClient;
    @Autowired
    private BusiNoGenerator busiNoService;


    @Override
    public synchronized String getBusiNo(String preffix, int seqLen, @Nullable Map<String, String> preffixPrarm) {
        Preconditions.checkArgument(StringUtils.isNotBlank(preffix));
        Preconditions.checkArgument(seqLen > 0);


        String no = busiNoService.getBusiNoFromCache(preffix);
        if (null == no) {
            busiNoService.bacthGenNo(preffix, seqLen);
            no = busiNoService.getBusiNoFromCache(preffix);
        }

        return PlaceholderUtil.format(no, preffixPrarm);
    }


}
